package com.rljk.controller.backend;/**
 * Created by mac on 2018/8/16.
 */

import com.rljk.common.controller.BaseController;
import com.rljk.common.result.CodeMsg;
import com.rljk.common.result.Result;
import com.rljk.domain.common.PageData;
import com.rljk.service.NoticeInfoManager;
import com.rljk.service.NoticeManager;
import com.rljk.utils.AppUtil;
import com.rljk.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName NoticeController
 * @Description 通知接口
 * @Author zhang
 * @Date 2018/8/16 9:48
 */
@RestController
@RequestMapping("/backendnotice")
@Slf4j
public class NoticeController extends BaseController {

    @Resource(name = "noticeService")
    private NoticeManager noticeService;

    @Resource(name = "noticeInfoService")
    private NoticeInfoManager noticeInfoService;

    /**
     * @Author zhang
     * @Description 后台获取所有的通知
     * @Date 2018/8/16 9:54
     * @Param [request]
     * @return java.lang.Object
     **/
    @RequestMapping(value="/alllist")
    public Object allNotice() {
        log.info("后台获取所有的通知");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("ALLLIST", pd.getString("FKEY"))) {
//                if (AppUtil.checkParam("allNotice",pd)){

                    List<PageData> allNotice = noticeService.allNotice(pd);
                    map.put("ALLNOTICE",allNotice);

                    return Result.success(map);
//                }else {
//                    return Result.error(CodeMsg.PARAM_LACK);
//                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * @Author zhang
     * @Description  修改通知是否显示
     * @Date 2018/8/16 13:12
     * @Param []
     * @return java.lang.Object
     **/
    @RequestMapping(value="/editisshow")
    public Object editIsShow() {
        log.info("修改通知是否显示");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("EDITNOTICEISSHOW", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("editNoticeIsShow",pd)){
                    noticeInfoService.edit(pd);
                    return Result.success(null);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }

    /**
     * @Author zhang
     * @Description 修改通知
     * @Date 2018/8/16 15:35
     * @Param []
     * @return java.lang.Object
     **/
    @RequestMapping(value="/editnotice")
    public Object editNotice() {
        log.info("修改通知");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("EDITNOTICE", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("editNotice",pd)){
                    noticeService.edit(pd);
                    return Result.success(null);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * @Author zhang
     * @Description  添加通知
     * @Date 2018/8/16 15:25
     * @Param []
     * @return java.lang.Object
     **/
    @RequestMapping(value="/addnotice")
    public Object addNotice() {
        log.info("添加通知");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("ADDNOTICE", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("addNotice",pd)){
                    pd.put("NOTICE_ID",this.getSnowId());
                    noticeService.save(pd);
                    return Result.success(null);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


}
